package com.hongning.railway.ui.main.fragments.college;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableInt;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.view.View;

import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqVideoBean;
import com.hongning.railway.entity.resbean.BaseRes;
import com.hongning.railway.entity.resbean.ResVideoBean;
import com.hongning.railway.ui.main.fragments.college.itemviewModel.LessonItemViewModel;
import com.hongning.railway.ui.main.fragments.college.itemviewModel.VideoItemViewModel;
import com.hongning.railway.ui.uichangeobserable.ListUIChangeObserable;
import com.hongning.railway.util.Apputils;

import java.util.List;

import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class LearnCompleteFragmentViewModel extends BaseViewModel<AppRepository>{
    public int page=1;
    public ReqVideoBean videoBean = new ReqVideoBean();
    public ObservableList<MultiItemViewModel> viewlist=new ObservableArrayList<>();

    public ObservableInt liststatus = new ObservableInt(View.GONE);

    public ListUIChangeObserable uc = new ListUIChangeObserable();
    public ItemBinding<MultiItemViewModel> itemBinding=ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            int type = (int) item.getItemType();
            if (type==2){
                itemBinding.set(BR.viewModel, R.layout.item_video_lesson);
            }else {
                itemBinding.set(BR.viewModel,R.layout.item_normal_lesson);
            }
        }
    });

    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            page=1;
            getData();
        }
    });

    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getData();
        }
    });
    public LearnCompleteFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
        initMessager();
    }

    public void getData(){

        videoBean.setAppIsTop("Y");
        videoBean.setIsLearned("1");
        videoBean.setUserId(Apputils.getUserId());
        videoBean.setPageNum(page);
        videoBean.setPageSize(Constant.PAGE_SIZE);
        model.getVideoList(videoBean)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new ApiDisposableObserver<BaseRes<List<ResVideoBean>>>() {
                    @Override
                    public void onResult(BaseRes<List<ResVideoBean>> resVideoBeans) {
                        if (page==1){
                            viewlist.clear();
                            uc.finishRefresh.call();
                            if (resVideoBeans.getData().isEmpty()){
                                uc.contentEmpty.call();
                                liststatus.set(View.VISIBLE);
                                return;
                            }else {
                                liststatus.set(View.GONE);
                            }
                        }else {
                            uc.finishLoadMore.call();
                        }
                        if (resVideoBeans.getData().size()<Constant.PAGE_SIZE){
                            uc.finishLoadMoreEnd.call();
                        }
                        page++;
                        for (ResVideoBean item:resVideoBeans.getData()) {
                            if (item.getType()==1){
                                viewlist.add(new LessonItemViewModel(LearnCompleteFragmentViewModel.this,item,true));
                            }else {
                                viewlist.add(new VideoItemViewModel(LearnCompleteFragmentViewModel.this,item,true));
                            }

                        }
                    }
                });

    }

    public void initMessager(){
        Messenger.getDefault().register(this,  Constant.LESSON_REFRESH, new BindingAction() {
            @Override
            public void call() {
                page=1;
                getData();
            }
        });
    }
}
